package  
{
	import flash.display.DisplayObject;
	import flash.display.DisplayObjectContainer;
	import flash.events.MouseEvent;
	import flash.display.Loader;
	import flash.net.URLRequest;
	import flash.events.Event;
	/**
	 * ...
	 * @author DarkGoodWIN
	 */
	
	public class TFriendsContainer extends TBaseContainer
	{
		private var GameInfo: TGameInfo = null;
		private var fClickEvent: Function = null;
				
		private function OnFriendClick(e: MouseEvent): void
		{
			var C: TControl = FindControlByObjectTarget(e.target);
			if (C != null)
			  GameInfo.GoToID(C.Tag, C.Hint)
			else
			  new TModalMessage("Неверный ID");
		}
		
		private function OnGetFrieds(O: Object): void
		{
			for (var key:Object in O)
            {
			  Lock();
			  var photoLoader:Loader = new Loader();
			  var url: URLRequest = new URLRequest(O[key]['photo_medium'].toString());

			  photoLoader.contentLoaderInfo.addEventListener(Event.COMPLETE, onLoaded);

			  GameInfo.AddRequest();
			  photoLoader.load(url);
			  
              var C: TControl = new InvControl(null, this, 0, 0, photoLoader);
			  C.Hint = O[key]['first_name'].toString() + " " + O[key]['last_name'].toString();
			  C.Tag = O[key]['uid'];
			  C.addEventListener(MouseEvent.CLICK, fClickEvent);
			  
			  AddControl(C);
			}
		}
		
	    private function onLoaded(e:Event):void {
			UnLock();
			if (!(Locked()))
			{
			  GameInfo.InitGame();
			}
			GameInfo.RemoveRequest();
			CalcAllBounds();
		}

		
		public function TFriendsContainer(Parent: TControl, aX: Number, anY: Number, aWidth: Number, aHeight: Number, aGameInfo: TGameInfo,
		       ClickEvent: Function = null, aVert: Boolean = false, InitOnCreate: Boolean = true)
		{
			super(Parent, aX, anY, aWidth, aHeight);
			GameInfo = aGameInfo;
			GameInfo.getUserInfos(OnGetFrieds);
			fClickEvent = ClickEvent;
			if (fClickEvent == null)
				fClickEvent = OnFriendClick;
				
			Vert = aVert;
			Scrolling = new ButtonScrolling(this);
			if (InitOnCreate)
				Init();		
		}
		
	}
}